package com.example.androidcompletesec.xmlparser;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import com.example.androidcompletesec.R;

public class Pull_parserActivity extends Activity {
	ListView list;
	ProgressDialog progressDlg;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.network);
		list = (ListView) findViewById(R.id.listView);
		new ParsingXml().execute("order.xml");
	}
	private class ParsingXml extends AsyncTask<String, Void, ArrayList<Item>> {
		protected void onPreExecute() {
			progressDlg = ProgressDialog.show(Pull_parserActivity.this, "Wait",
					"order.xml & order.json Making ...");
		}
		protected void onPostExecute(ArrayList<Item> result) {
			MyAdapter adapter = new MyAdapter(Pull_parserActivity.this,
					R.layout.itemrow, result);
			progressDlg.dismiss();
			list.setAdapter(adapter);
		}
		protected ArrayList<Item> doInBackground(String... params) {
			StringBuffer sb = new StringBuffer();
			String str = null;
			try {
				BufferedReader fis = new BufferedReader(new InputStreamReader(
						openFileInput(params[0])));
				while ((str = fis.readLine()) != null) {
					sb.append(str);
				}
				fis.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return parsingXml(sb.toString());
		}
	}
	public ArrayList<Item> parsingXml(String xml) {
		ArrayList<Item> arItem = new ArrayList<Item>();
		Item itemClass = null;
		boolean initItem = false;
		try {
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			XmlPullParser parser =  factory.newPullParser();
			
			parser.setInput(new StringReader(xml));
			
			int eventType=  parser.getEventType(); //엘리먼트가 생길때마다 추가..되어진다
			
			while((eventType = parser.getEventType()) !=XmlPullParser.END_DOCUMENT){
				switch(eventType){
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					if(parser.getName().equalsIgnoreCase("item")){//start tag가 아이템이라면..
						initItem = true;
						itemClass = new Item();
					}
					if(parser.getAttributeCount() > 0){
						for(int i = 0 ; i<parser.getAttributeCount(); i++){
							if(parser.getAttributeName(i).equalsIgnoreCase("price")){
								itemClass.setItemPrice(Integer.parseInt(parser.getAttributeValue(i)));
							}else{
								itemClass.setMakerName(parser.getAttributeValue(i));
							}
						}
					}
					break;
				case XmlPullParser.TEXT:
					if(initItem){
						itemClass.setItemName(parser.getText());
						initItem = false;
					}
					break;
				case XmlPullParser.END_TAG:
					if(parser.getName().equalsIgnoreCase("item")){
						arItem.add(itemClass);
					}
					break;
				}
				eventType = parser.next();
			}
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return arItem;
	}
	
	
}
